#include "hwps_regist_form.h"
#include "ui_hwps_regist_form.h"
#include"QTableWidget"
#include"QTimer"
#include"data.h"
#include"QThread"
#include<QMessageBox>

HwPs_Regist_Form::HwPs_Regist_Form(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HwPs_Regist_Form)
{
    ui->setupUi(this);

    ui->tableWidget_hwPsswitch->setRowCount(1000);
    ui->tableWidget_hwPsswitch->verticalHeader()->setHidden(true);
    ui->tableWidget_regist->verticalHeader()->setHidden(true);

    for (int i = 0; i < ui->tableWidget_hwPsswitch->rowCount(); ++i) {
        ui->tableWidget_hwPsswitch->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
        ui->tableWidget_hwPsswitch->setItem(i,1,new QTableWidgetItem(QString::number(i*10)));
    }



    QTimer *timer=new QTimer(this);
    timer->setInterval(300);
    timer->start();
    connect(timer,&QTimer::timeout,this,&HwPs_Regist_Form::timerOut);
    connect(this,&HwPs_Regist_Form::runFinishSignal,this,[&](){
        ui->pushButton_2->setEnabled(true);
    });

    connect(this,&HwPs_Regist_Form::registAddRow,this,[&](float value){

        ui->tableWidget_regist->setRowCount(ui->tableWidget_regist->rowCount()+1);
        ui->tableWidget_regist->setItem(ui->tableWidget_regist->rowCount()-1,0,new QTableWidgetItem(QString::number(ui->tableWidget_regist->rowCount()+1)));
        ui->tableWidget_regist->setItem(ui->tableWidget_regist->rowCount()-1,1,new QTableWidgetItem(QString::number(value)));
    });
}

HwPs_Regist_Form::~HwPs_Regist_Form()
{
    registIsRun=false;
    delete ui;
}

void HwPs_Regist_Form::timerOut(){
    if(Data::CardIsConnect()){
        uint opValue=0;
        float dpos=0;
        PMC_GetOp(Data::handle,ui->spinBox_hwOp->value(),&opValue);
        PMC_GetDpos(Data::handle,ui->spinBox_hwAxis->value(),&dpos);

        ui->checkBox_opState->setChecked((bool)opValue);
        ui->label_currentPos->setText(QString::number(dpos));

    }
}

void HwPs_Regist_Form::on_pushButton_2_clicked()
{
    if(Data::CardIsConnect()){
        // int enable1=0;
        // int enable2=0;
        // PMC_GetAxisEnable(Data::handle,ui->spinBox_hwOp->value(),&enable1);
        // PMC_GetAxisEnable(Data::handle,ui->spinBox_hwAxis->value(),&enable1);
        // if(enable1==0||enable2==0){
        //     QMessageBox::about(NULL,"Warn","请查看比较轴，编码轴使能情况！！");
        // }
        ui->pushButton_2->setEnabled(false);
        std::thread thr([&](){
            PMC_SetAtype(Data::handle,ui->spinBox_hwAxis->value(),1);
            PMC_SetUnits(Data::handle,ui->spinBox_hwAxis->value(),ui->doubleSpinBox_units->value());
            PMC_SetSpeed(Data::handle,ui->spinBox_hwAxis->value(),ui->doubleSpinBox_spd->value());
            PMC_SetAccel(Data::handle,ui->spinBox_hwAxis->value(),ui->doubleSpinBox_accel->value());
            PMC_SetDecel(Data::handle,ui->spinBox_hwAxis->value(),ui->doubleSpinBox_decel->value());
            PMC_SetDpos(Data::handle,ui->spinBox_hwAxis->value(),ui->doubleSpinBox_startPos->value());
            if(ui->checkBox_enableHw->isChecked()){
                if(ui->spinBox_hwStartPoint->value()<ui->spinBox_hwEndPoint->value()){
                    float value=0;
                    if(ui->comboBox_RegistMode->currentIndex()==0){
                        for (int i = 0; i < ui->spinBox_hwEndPoint->value()-ui->spinBox_hwStartPoint->value(); ++i) {
                            value=ui->tableWidget_hwPsswitch->item(ui->spinBox_hwStartPoint->value()+i,1)->text().toFloat();
                            PMC_SetTable(Data::handle,i,1,&value);
                        }
                    }else{
                        for (int i = 0; i < ui->spinBox_hwEndPoint->value()-ui->spinBox_hwStartPoint->value(); ++i) {
                            value=ui->tableWidget_hwPsswitch->item(ui->spinBox_hwStartPoint->value()+i,1)->text().toFloat();
                            PMC_SetTable(Data::handle,i,1,&value);
                        }
                    }

                    PMC_HwPswitch2(Data::handle,ui->spinBox_hwAxis->value(),2,0,0,0,0,0,0);
                    PMC_HwPswitch2(Data::handle,ui->spinBox_hwAxis->value(),1,ui->spinBox_hwOp->value()
                                   ,ui->comboBox_hwOpState->currentIndex()
                                   ,0,(ui->spinBox_hwEndPoint->value()-ui->spinBox_hwStartPoint->value())-1,
                                   ui->comboBox_hwDir->currentIndex(),0);
                }
            }
            if(ui->checkBox_hwOpInvert->isChecked()){
                if(ui->spinBox_hwOp->value()>=0){
                    PMC_HwTimer(Data::handle,0,0,0,0,0,0);
                    PMC_HwTimer(Data::handle,2,ui->spinBox_hwTimer1->value(),
                                ui->spinBox__hwTimer2->value(),
                                ui->spinBox__hwTimer3->value(),
                                ui->comboBox_hwOpState->currentIndex(),
                                ui->spinBox_hwOp->value());
                }
            }
            QThread::msleep(200);
            PMC_Single_MoveAbs(Data::handle, ui->spinBox_hwAxis->value()
                               , ui->doubleSpinBox_endPos->value());
            int idle=0;int ret=0;
            while (idle==0 && ret==0) {
                ret=PMC_GetIfIdle(Data::handle,ui->spinBox_hwAxis->value(),&idle);
                QThread::msleep(1);
            }
            emit runFinishSignal();
        });
        thr.detach();
    }
}


void HwPs_Regist_Form::on_pushButton_3_clicked()
{
    if(Data::CardIsConnect()){
        PMC_Rapidstop(Data::handle,3);
    }
}


void HwPs_Regist_Form::on_pushButton_registStart_clicked()
{
    if(!Data::CardIsConnect()){

        ui->pushButton_registStart->setText(tr("启动锁存"));
        return;
    }
    if(!registIsRun){
        ui->tableWidget_regist->setRowCount(0);
        registIsRun=true;
        ui->pushButton_registStart->setText(tr("停止锁存"));
        int mode=0;
        if(ui->comboBox_RegistMode->currentIndex()==0){
            mode=3;
        }else if(ui->comboBox_RegistMode->currentIndex()==1){
            mode=4;
        }else if(ui->comboBox_RegistMode->currentIndex()==2){
            mode=13;
        }else if(ui->comboBox_RegistMode->currentIndex()==3){
            mode=14;
        }
        // PMC_CycleRegist(Data::handle,ui->spinBox_registAxis->value(),
        //                 mode,1100,5);

        auto task=[&](int mode,int axis){
            while (registIsRun) {
                PMC_Regist(Data::handle,axis,mode);
                int mark=0;
                float pos=0;
                int ret=0;
                if(mode==3 || mode==4){
                    while (mark == 0 && registIsRun == true && ret==0)
                    {
                        ret=PMC_GetMark(Data::handle, axis, &mark);
                        if (!registIsRun)
                            return;
                    }
                    PMC_GetRegPos(Data::handle, axis, &pos);
                }else{
                    while (mark == 0 && registIsRun == true && ret==0)
                    {
                        ret=PMC_GetMarkB(Data::handle, axis, &mark);
                        if (!registIsRun)
                            return;
                    }
                    PMC_GetRegPosB(Data::handle, axis, &pos);
                }
                emit registAddRow(pos);
            }
        };
        std::thread thr(task,mode,ui->spinBox_registAxis->value());
        thr.detach();
    }else{
        registIsRun = false;
        ui->pushButton_registStart->setText(tr("启动锁存"));
    }
}

